﻿using System;
using System.Collections.Generic;
using System.Reflection;
using System.Text;

namespace System.Reflection
{
    public static class TypeExtensions
    {
        /// <summary>
        /// Get custom attributes
        /// </summary>
        /// <typeparam name="TAttr"></typeparam>
        /// <param name="type"></param>
        /// <param name="inherit"></param>
        /// <returns></returns>
        public static TAttr[] GetCustomAttributes<TAttr>(this Type type, bool inherit) where TAttr : Attribute
        {
            return (TAttr[])type.GetCustomAttributes(typeof(TAttr), inherit);
        }

        /// <summary>
        /// Get custom attribute
        /// </summary>
        /// <typeparam name="TAttr"></typeparam>
        /// <param name="type"></param>
        /// <param name="inherit"></param>
        /// <returns></returns>
        public static TAttr GetCustomAttribute<TAttr>(this  Type type, bool inherit) where TAttr : Attribute
        {
            TAttr[] attrs = type.GetCustomAttributes<TAttr>(inherit);
            if (attrs != null && attrs.Length > 0)
                return attrs[0];
            else
                return null;
        }
    }
}
